package com.linglingC.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linglingC.mapper.ResidentMapper;
import com.linglingC.pojo.PageBean;
import com.linglingC.pojo.Resident;
import com.linglingC.pojo.ResidentNameId;
import com.linglingC.service.ResidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ResidentServiceImpl implements ResidentService {

    @Autowired
    private ResidentMapper residentMapper;

    @Override
    public PageBean findAll(Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<Resident> list=residentMapper.findAll();

        Page<Resident> p=(Page<Resident>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public PageBean select(String key, String value, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Resident resident=setKeyValue(key,value);

        List<Resident> list=residentMapper.selectP(resident);

        Page<Resident> p=(Page<Resident>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public void addResident(Resident resident) {
        residentMapper.addResident(resident);
    }

    @Override
    public void deleteOneResident(Integer id) {
        residentMapper.deleteOneResident(id);
    }

    @Override
    public void editResident(Resident resident) {
        residentMapper.editResident(resident);
    }

    @Override
    public List<ResidentNameId> selectNameID() {
        List<ResidentNameId> r=residentMapper.findNameId();
        return r;
    }

    private Resident setKeyValue(String key, String value)
    {
        Resident resident=new Resident();
        switch (key){
            case "ID":
                resident.setID(Integer.parseInt(value));
                break;
            case "姓名":
                resident.setName(value);
                break;
            case "性别":
                resident.setGender(value);
                break;
            case "电话号码":
                resident.setTelephone(value);
                break;
            case "room":
                resident.setRoom(value);
                break;
            case "checkin":
                resident.setCheckin(LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                break;
            default:
                break;
        }
        return resident;
    }
}
